package com.qdxwx.manage.controller.dict;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.data.service.dict.DictDataService;
import com.qdxwx.data.service.dict.DictTypeService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;

import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.dict.DictData;
import com.qdxwx.models.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * 数据字典信息
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/dict/data")
public class DictDataController extends BaseController {

    private String prefix = "dict/data";
    @Autowired
    private DictDataService dictDataService;
    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 查询字典详细
     */
    @Permissions("dict:list")
    @GetMapping("/detail/{dictId}")
    public String detail(@PathVariable("dictId") Long dictId, ModelMap mmap) {
        if (dictId < 1) return error404;
        mmap.put("dict", dictTypeService.getById(dictId));
        mmap.put("dictList", dictTypeService.getAll());
        return prefix + "/data";
    }

    @PostMapping("/list.json")
    @Permissions("dict:list")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request, DictData dictData) {
        List<DictData> list = dictDataService.getList(dictData, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(dictDataService.count(dictData));
        return rspData;
    }

    /**
     * 新增字典类型
     */
    @GetMapping("/add/{dictType}")
    public String add(@PathVariable("dictType") String dictType, ModelMap mmap) {
        mmap.put("dictType", dictType);
        return prefix + "/add";
    }

    /**
     * 新增保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @Permissions("dict:add")
    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(@Validated DictData dict) {
        dict.setCreateBy(SessionUtils.getLoginName());
        return toAjax(dictDataService.insert(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{dictCode}")
    public String edit(@PathVariable("dictCode") Long dictCode, ModelMap mmap) {
        mmap.put("dict", dictDataService.getById(dictCode));
        return prefix + "/edit";
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @Permissions("dict:edit")
    @PostMapping("/edit.json")
    @ResponseBody
    public AjaxResult editSave(@Validated DictData dict) {
        dict.setUpdateBy(SessionUtils.getLoginName());
        return toAjax(dictDataService.update(dict));
    }

    @Log(title = "字典数据", businessType = BusinessType.DELETE)
    @Permissions("dict:remove")
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dictDataService.deleteByIds(ids));
    }
}
